package ynu.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import ynu.edu.mapper.IUserMapper;
import ynu.edu.po.User;
import ynu.edu.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private IUserMapper userMapper;

    @Override
    public User getUserByIdByPass(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", user.getUserId()).eq("password", user.getPassword());
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean getUserById(String userId) {
        return userMapper.selectById(userId) != null;
    }

    @Override
    public boolean saveUser(User user) {
        if (userMapper.selectById(user.getUserId()) != null) {
            throw new RuntimeException("User already exists with ID: " + user.getUserId());
        }
        return userMapper.insert(user) > 0;
    }
}
